﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Model.TrainModelHelper;

namespace DAL.TrainDALhelp
{
    public class ResultDAL
    {
        private static string ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
      SqlConnection conn = new SqlConnection(ConStr);
        public static List<ResultModel> StaticResultList = new List<ResultModel>();




        public int ResuListCount()
        {
            int count;
            using (SqlConnection conn = new SqlConnection(ConStr))
            {
                conn.Open();
                string sql = "select Count(*) from  ResultTable";
                SqlCommand command = new SqlCommand(sql, conn);
                count = (int)command.ExecuteScalar();
            }
            return count;
        }
        public List<ResultModel> ResultList()
        {
            if (StaticResultList == null || StaticResultList.Count != ResuListCount())
            {
                StaticResultList.Clear();
                conn.Open();
                SqlCommand comd = new SqlCommand("select * from  ResultTable", conn);
                SqlDataReader read = comd.ExecuteReader();

                while (read.Read())
                {
                    ResultModel str = new ResultModel();

                    str.ItemId = Convert.ToInt32(read["ItemId"]);
                    str.ResultName = read["ResultName"].ToString();
                    str.ResultId = Convert.ToInt32(read["ResultId"]);

                    str.ResulProId = Convert.ToInt32(read["ResulProId"]);
                    StaticResultList.Add(str);


                }

                read.Close();
                conn.Close();
            }
                return StaticResultList;
            
        }
       
       
        public int ExamResult(string ItemId, string ResultId)
        {
            conn.Open();
            SqlCommand comd = new SqlCommand("select SUM(ResulProId) from  ResultTable  where ItemId in(" + ItemId + ")and ResultId in (" + ResultId + ")", conn);
            var I = comd.ExecuteScalar();

            conn.Close();
             int A=  Convert.ToInt32(I);
           return A;
        }
    }
}
